package com.demo.controller;

import com.demo.entity.CartInfoList;
import com.demo.entity.Orders;
import com.demo.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.List;

@Controller // 加入IOC容器
@RequestMapping("controller")
public class OrdersController {
	// 控制器依赖于Service
	@Autowired
	@Qualifier("ordersService")
	private OrdersService ordersService;

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}

	// 查询所有订单信息列表
	@RequestMapping({ "order_list_action", "order_info_list_action" })
	public String queryAllOrdersInfo(HttpServletRequest request) {
		System.out.println("查询所有订单信息列表.....................");
		List<Orders> orders = ordersService.queryOrdersInfo();
		request.getSession().setAttribute("ordersInfo", orders);
		if (request.getRequestURI().endsWith("/order_list_action")) {
			return "orders_list";
		} else if (request.getRequestURI().endsWith("order_info_list_action")) {
			return "ordersListManage";
		}
		return null;
	}

	// 未支付订单的支付操作-同时减少相应的库存量
	@RequestMapping({"updatePayOrdersById/{id}","updatePayOrdersByManage/{id}"})
	public String payOrdersByState(@PathVariable("id") String id, HttpServletRequest request) {
		CartInfoList orderProducts = (CartInfoList) request.getSession().getAttribute("orderProductsInfo");
		if (orderProducts != null) {
			int buynum = orderProducts.getP_buynum(); // 采购数量-库存减少量
			String pid = (String) request.getSession().getAttribute("pid"); // 采购商品ID

			// 先查询库存量-再减少
			int odnum = ordersService.selectProductPnum(pid);
			System.out.println("根据购买数量减少库存量...." + buynum + "\t" + odnum);
			if (odnum >= buynum) {
				ordersService.updateProductsInfo(buynum, pid, buynum); // 根据购买数量减少库存量-----buynum可删减
				System.out.println("库存量减少cg................");
				ordersService.updateOrdersByState(id); // 设置订单状态为已支付
			} else {
				System.out.println("库存量少于下单量，操作失败............");
			}
		}
		if(request.getRequestURI().endsWith("updatePayOrdersById/"+id)){
			return "redirect:/controller/order_list_action";
		}else if(request.getRequestURI().endsWith("updatePayOrdersByManage/"+id)){
			return "redirect:/controller/order_info_list_action";
		}
		return null;
	}

	// 插入订单数据
	@RequestMapping({"orderInfo","orderInfoByManage"})
	public String insertOrderInfomation(HttpServletRequest req) throws UnsupportedEncodingException {
		String pname = new String(req.getParameter("pname").getBytes("iso-8859-1"), "UTF-8"); // 商品名
		int quantity = Integer.parseInt(req.getParameter("quantity")); // 购买数量
		Double price = Double.parseDouble(req.getParameter("price")); // 商品单价
		// 将采购的商品信息存储在session中
		CartInfoList orderProductsInfo = new CartInfoList();
		orderProductsInfo.setP_name(pname);
		orderProductsInfo.setP_price(price);
		orderProductsInfo.setP_buynum(quantity);
		req.getSession().setAttribute("orderProductsInfo", orderProductsInfo);
		// 将采购的商品id存入session中
		// int pid = Integer.parseInt(req.getParameter("pid"));
		String pid = req.getParameter("pid");
		req.getSession().setAttribute("pid", pid);

		DecimalFormat df = new DecimalFormat("#.00");

		Double money = Double.parseDouble(df.format(quantity * price)); // 订单费用-总价格
		String receiverName = new String(req.getParameter("rename").getBytes("iso-8859-1"), "UTF-8"); // 收件人
		String receiverPhone = req.getParameter("rephone"); // 收件号码
		String receiverAddress = new String(req.getParameter("readdress").getBytes("iso-8859-1"), "UTF-8"); // 收件地址
		int paystate = 0; // 支付状态
		String uname = (String) req.getSession().getAttribute("USERNAME"); // 用户名
		System.out.println("uname：" + uname);
		ordersService.insertOrderInfo(money, receiverName, receiverPhone, receiverAddress, paystate, uname);
		System.out.println("插入订单数据cg...............");

		if(req.getRequestURI().endsWith("orderInfo")){
			return "redirect:/controller/order_list_action";
		}else if(req.getRequestURI().endsWith("orderInfoByManage")){
			return "redirect:/controller/order_info_list_action";
		}
		return null;
	}

	// 删除-撤销订单
	@RequestMapping({ "deleteUnPayOrdersById/{id}", "deleteOrderById/{id}" })
	public String deleteUnPayOrdersById(@PathVariable("id") String id, HttpServletRequest request) {
		ordersService.deleteUnPayOrdersById(id);
		System.out.println("撤销成功................");
		if (request.getRequestURI().endsWith("/deleteUnPayOrdersById/" + id)) {
			return "redirect:/controller/order_list_action";
		} else if (request.getRequestURI().endsWith("deleteOrderById/" + id)) {
			return "redirect:/controller/order_info_list_action";
		}
		return null;
	}

}